/* upms.js 用户和应用查询服务 by zds 2019-06-04 */
const md5 = require('md5');
const config = reqlib('/config/config.js');

// 应用缓存
var gAppSites = {};
var gSites = {};
var gSiteScopes = {};
var gPasswordSalt = '20201103';

module.exports = {

	// 查询用户信息
	find: async function(db, userId){
		if (!userId)
			return null;
		let user = null;
		// 查询 t_user 表中的用户
		const [users] = await db.query('SELECT * FROM t_user WHERE id=?', userId);
		if (users.length > 0){
			user = users[0];
			// 不保存密码
			if (user.password)
				user.password = '******';
		}
		return user;
	},

	// 密码编码算法
	password: function(secret, salt){
		if (!secret)
			return '';
		if (!salt) salt = gPasswordSalt;
		return ('fmsc2024' + md5(secret + salt)).toUpperCase();
	},

	// 验证用户登录
	verifyLogin: async function(db, account, password){
		const [users] = await db.query('SELECT * FROM t_user WHERE account=?', account);
		if (users.length < 1)
			throw new Error('用户' + account + '不存在');
		let user = users[0];
		// 验证密码是否正确
		let verify = false;
		if (user.source && user.source != 'local'){
			// 第三方平台使用测试密码登录
			if (password == config.master_secret)
				verify = true;
		} else if (user.password){
			if (password){
				let pwd = this.password(password, account);

				verify = (user.password == pwd);
				// 不保存密码
				user.password = '******';
			}
		} else if (!password){
			verify = true;
		}
		// 暂时不验证密码
		if (!verify)
			throw new Error('账号或密码错误');
		// 保存登录时间
		await db.query('UPDATE t_user SET login_time=? WHERE id=?', [new Date(), user.id]);
		return user;
	},
	
	// 获取应用信息
	getSite: async function (db, id){
		let site = gSites[id];
		if (site)
			return site;
		const [rows] = await db.query("SELECT * FROM t_site WHERE id=?", id);
		if (rows.length > 0){
			site = rows[0];
			gSites[id] = site;
		}
		return site;
	},
	getAppSite: async function(db, appId){
		let site = gAppSites[appId];
		if (site)
			return site;
		const [rows] = await db.query("SELECT * FROM t_site WHERE app_id=?", appId);
		if (rows.length > 0){
			site = rows[0];
			gAppSites[appId] = site;
		}
		return site;
	},
	// 获取应用权限
	getSiteScope: async function(db, id){
		let scope = gSiteScopes[id];
		if (scope)
			return scope;
		let site = await this.getSite(db, id);
		if (site && site.owner){
			let user = await this.find(db, site.owner);
			scope = user.type;
			gSiteScopes[id] = scope;
		}
		return scope;
	},
	// 设置应用信息
	setSite: function(site){
		gSites[site.id] = site;
		gAppSites[site.app_id] = site;
		gSiteScopes[site.id] = null;
	},
	// 判断用户是否为应用管理员
	isOwner: async function (db, siteId, userId){
		let site = await this.getSite(db, siteId);
		return site && site.owner == userId;
	},

	// 跳转到用户登录页面
	toLogin: function(ctx, message, extra){
		if (extra == 'json')
			return ctx.body = {code: 1005, message};
		let jumpUrl = '/login.html?message=' + encodeURIComponent(message);
		if (extra)
			jumpUrl += extra;
		return ctx.redirect(jumpUrl);
	},

	// 显示提示信息页面
	showInfo: function(ctx, code, message){
		let jumpUrl = '/info.html?code=' + code + 'message=' + encodeURIComponent(message);
		return ctx.redirect(jumpUrl);
	},

}